//------函数声明------//
void handleUserRequest();
bool handleFileRead(String resource);
String getContentType(String filename);
void CollectMsg();
void SendInStatus();
void SendOutStatus();
void SendTimeStatus();
void SendMsgStatus();

//----------------------处理网页 函数实现部分-------------------------//
void httpRecive()
{
    server.on("/Collect", HTTP_GET, CollectMsg);        //
    server.on("/InStatus", HTTP_GET, SendInStatus);     //
    server.on("/OutStatus", HTTP_GET, SendOutStatus);   //
    server.on("/TimeStatus", HTTP_GET, SendTimeStatus); //
    server.on("/Othermsg", HTTP_GET, SendMsgStatus);    //

    server.onNotFound(handleUserRequest); // 处理其它网络请求
}

//------------收集各个客户端发送的信息整合--------//
void CollectMsg()
{
    unsigned short ID = server.arg("ID").toInt(); // 获取客户端发送HTTP
    server.send(200, "text/plain", "Received");   // 发送http响应

    Timelabel[ID - 1] = server.arg("Time");
    BeeInnumber[ID - 1] = server.arg("InNumber");
    BeeOutnumber[ID - 1] = server.arg("OutNumber");
    g_Temp[ID - 1] = server.arg("Temp") + "C";
    g_Hum[ID - 1] = server.arg("Hum") + "%";
    DiskFree[ID - 1] = server.arg("DiskFree") + "%";

    

    transflag = ID - 1; // 接收到来自客户端信息
    httpflag = true;
}

//---------发送给网页设备状态----------//
void SendInStatus()
{
    if (httpflag)
    {
        unsigned short ID = server.arg("ID").toInt();
        server.send(200, "text/plain", BeeInnumber[ID - 1]); //发送模拟输入引脚到客户端ajax请求
    }
    else
    {
        server.send(304); // 文档未更新
    }
}

void SendOutStatus()
{
    if (httpflag)
    {
        unsigned short ID = server.arg("ID").toInt();
        server.send(200, "text/plain", BeeOutnumber[ID - 1]); //发送模拟输入引脚到客户端ajax请求
    }
    else
    {
        server.send(304);
    }
}

void SendTimeStatus()
{
    if (httpflag)
    {
        unsigned short ID = server.arg("ID").toInt();
        server.send(200, "text/plain", Timelabel[ID - 1]); //发送模拟输入引脚到客户端ajax请求
    }
    else
    {
        server.send(304);
    }
}

void SendMsgStatus()
{
    if (httpflag)
    {
        unsigned short ID = server.arg("ID").toInt();
        server.send(200, "text/plain", "Temp:" + g_Temp[ID - 1] + "  Hum:" + g_Hum[ID - 1] + "  TF:" + DiskFree[ID - 1]); //发送模拟输入引脚到客户端ajax请求
        httpflag = false;
    }
    else
    {
        server.send(304);
    }
}

//----------------------处理网页-------------------------//
void handleUserRequest()
{

    String reqResource = server.uri();
    // 通过handleFileRead函数处处理用户请求资源
    bool fileReadOK = handleFileRead(reqResource);
    // 如果在SPIFFS无法找到用户访问的资源，则回复404 (Not Found)
    if (!fileReadOK)
    {
        server.send(404, "text/plain", "404 Not Found");
    }
}

bool handleFileRead(String resource) //处理浏览器HTTP访问
{
    if (resource.endsWith("/"))
    {                                 // 如果访问地址以"/"为结尾
        resource = "/html/show.html"; // 则将访问地址修改为/index.html
    }

    String contentType = getContentType(resource);  // 获取文件类型
    String content = tf.readFile(resource.c_str()); // 读取文件内容
    if (content != "")
    {
        server.send(200, contentType, content); // 发送文件内容
        return true;
    }
    return false; // 如果文件未找到，则返回false
}

// 获取文件类型
String getContentType(String filename)
{
    if (filename.endsWith(".htm"))
        return "text/html";
    else if (filename.endsWith(".html"))
        return "text/html";
    else if (filename.endsWith(".css"))
        return "text/css";
    else if (filename.endsWith(".js"))
        return "application/javascript";
    else if (filename.endsWith(".png"))
        return "image/png";
    else if (filename.endsWith(".gif"))
        return "image/gif";
    else if (filename.endsWith(".jpg"))
        return "image/jpeg";
    else if (filename.endsWith(".ico"))
        return "image/x-icon";
    else if (filename.endsWith(".xml"))
        return "text/xml";
    else if (filename.endsWith(".pdf"))
        return "application/x-pdf";
    else if (filename.endsWith(".zip"))
        return "application/x-zip";
    else if (filename.endsWith(".gz"))
        return "application/x-gzip";
    return "text/plain";
}